//
//  GeneralWebViewController.m
//  OMS
//
//  Created by goldeney on 14/11/13.
//  Copyright (c) 2014年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "CSGeneralWebViewController.h"

@interface CSGeneralWebViewController ()

@property (nonatomic, strong) NSString *textAndHtml;

@end

@implementation CSGeneralWebViewController
@synthesize webView = _webView;

- (id)initWithWebAndHtmlString:(NSString*)string title:(NSString*)title
{
    self = [super init];
    if (self) {
        self.textAndHtml = string;
        
        if (title && ![title isEqualToString:@""])
        self.title = title;
        else self.title = @"无标题";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.webView.scalesPageToFit = YES;
    _webView.scrollView.bouncesZoom = NO;
    
    //检测是否有数据显示
    if(!(self.textAndHtml && ![self.textAndHtml isEqualToString:@""])){
        return;
    }
    
    //判断字符串是否包含
    NSRange range = [self.textAndHtml rangeOfString:@"<html>"];
    NSRange rangeLater = [self.textAndHtml rangeOfString:@"<html>"];
    if (range.location != NSNotFound && rangeLater.location != NSNotFound){
        //是一个HTML文本，加载文本信息
        [self.webView loadHTMLString:self.textAndHtml baseURL:nil];
        
    } else {
        //不是一个HTML文本，加载文本信息
        NSMutableString * html = [NSMutableString string];
        [html appendFormat:@"<html>"
         @"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"
         @"<style>#center{width:32px; height:32px; position:absolute;}</style>"
         @"<meta name=\"viewport\" content=\"width=device-width, user-scalable=no\">"
         @"<body>%@</body></html>",self.textAndHtml];
        [self.webView loadHTMLString:html baseURL:nil];
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
